﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;
using System.Data.SQLite;


namespace Communergy.Repository.SQLite
{
    public class SQLiteRealtimeRepository : IRealtimeRepository
    {
        SQLiteEntities sqlite;

        public SQLiteRealtimeRepository(string connectionString)
        {
            sqlite = new SQLiteEntities(connectionString);
        }
        #region IRealtimeRepository Members

        public void Insert_ElectricityRealtime(string meterId, List<RealtimeEnergyReading> realtimeEnergyReadings)
        {
            long meterIdLong = Convert.ToInt32(meterId);

            foreach (var reading in realtimeEnergyReadings)
            {
                var sqliteReading = new ElectricityRealtime { 
                    RealtimeDateTime  = reading.ReadingDateTime,
                    RealtimeJoules = reading.Joules,
                    RealtimeWatts = reading.Watts,
                    RealtimeMeterId = meterIdLong
                };
                sqlite.AddToElectricityRealtime(sqliteReading);
            }
            sqlite.SaveChanges();
        }

        #endregion
    }
}
